import 'package:flutter/material.dart';

class PPHotSaleCommodityItem extends StatefulWidget {

PPHotSaleCommodityItem(Map data, double itemWidth, double itemHeight, { Key key, this.index, this.onPressAction }): this.data = data, this.itemWidth = itemWidth, this.itemHeight = itemHeight, super(key: key);

    final Map data;
    final int index;
    final double itemWidth;
    final double itemHeight;
    final Function onPressAction;

    _PPHotSaleCommodityItemState createState() => _PPHotSaleCommodityItemState();

}

class _PPHotSaleCommodityItemState extends State<PPHotSaleCommodityItem> {

    Map data;
    int index;
    double itemWidth;
    double itemHeight;

    @override
    void initState() {
        data = widget.data;
        index = widget.index;
        itemWidth = widget.itemWidth;
        itemHeight = widget.itemHeight;
        super.initState();
    }

    @override
    void didUpdateWidget(PPHotSaleCommodityItem oldWidget) {
        data = widget.data;
        index = widget.index;
        itemWidth = widget.itemWidth;
        itemHeight = widget.itemHeight;
        super.didUpdateWidget(oldWidget);
    }

    @override
    Widget build(BuildContext context) {
        String cmmdtyName = data != null && data['cmmdtyName'] != null ? data['cmmdtyName'] : '';
        cmmdtyName = cmmdtyName.length > 7 ? cmmdtyName.substring(0, 7) : cmmdtyName;
        String cmmtdyPicUrl = data['cmmtdyPicUrl'] != null ? data['cmmtdyPicUrl'] : '';
        cmmtdyPicUrl = cmmtdyPicUrl.startsWith('http') ? cmmtdyPicUrl : 'https://' + cmmtdyPicUrl;

        return (Container(
            child: FlatButton(
                padding: EdgeInsets.all(0),
                child: Container(
                    width: itemWidth,
                    height: itemHeight,
                    child: Column(
                        children: [
                            Image.network(cmmtdyPicUrl, width: itemWidth, fit: BoxFit.fitWidth),
                            Container(
                                width: itemWidth,
                                padding: EdgeInsets.fromLTRB(0, 5, 0, 5),
                                child: Text(cmmdtyName, textAlign: TextAlign.center, maxLines: 1, style: TextStyle( fontSize: 14, fontWeight: FontWeight.bold, color: Color.fromRGBO(51, 51, 51, 1) ))
                            ),
                            _getInstInfoView()
                        ],
                    ),
                ),
                onPressed: _onPressAction,
            ),
        ));
    }

    _getInstInfoView() {
        if(data != null && data['defaultInstInfo'] != null && data['defaultInstInfo'].indexOf('|') > 0) {
            List<String> arr = data['defaultInstInfo'].split('|');
            return (Container(
                width: itemWidth,
                child: Row(
                    children: [
                        Text('¥' + arr[0], style: TextStyle( fontSize: 14, fontWeight: FontWeight.w400, color: Color.fromRGBO(255, 170, 30, 1))),
                        Text('*' + arr[1] + '期', style: TextStyle( fontSize: 14, fontWeight: FontWeight.w400, color: Color.fromRGBO(102, 102, 102, 1)))
                    ],
                )
            ));
        } else {
            return null;
        }
    }


    _onPressAction() {
        if(widget.onPressAction != null) {
            widget.onPressAction(data, index);
        }
    }

}
